package com.pathpad.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.pathpad.dao.WebappDao;
import com.pathpad.hibernate.PPUser;
import com.pathpad.hibernate.PPUserCommonPad;
import com.pathpad.hibernate.SRWebsites;

/**
 * 首页显示
 * 
 * @author JiangZhen
 * 
 */
public class IndexController extends MultiActionController {

	private WebappDao webappDao;

	/**
	 * 没有登录的情况下显示系统推荐网站,否则显示用户自己的网站.
	 */
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<SRWebsites> srWebsitesList = webappDao.findSRWebsites();
		List<PPUserCommonPad> resultPPUserCommonPadList = new ArrayList<PPUserCommonPad>();
		if (null != request.getSession().getAttribute("userInfo")) {
			PPUser ppUser = (PPUser) request.getSession().getAttribute(
					"userInfo");
			resultPPUserCommonPadList = webappDao.findPPUserCommonPad(ppUser
					.getUserId());

			if (null != resultPPUserCommonPadList
					&& resultPPUserCommonPadList.size() != 0) {
				request.setAttribute("userWebResultList",
						resultPPUserCommonPadList);
			}
			request.setAttribute("indexWebsitesList", resultPPUserCommonPadList);
		} else {
			for (SRWebsites srWebsites : srWebsitesList) {
				PPUserCommonPad newPPpUserCommonPad = new PPUserCommonPad();
				newPPpUserCommonPad.setWebID(srWebsites.getWebID());
				newPPpUserCommonPad.setWebName(srWebsites.getWebName());
				newPPpUserCommonPad.setWebLocation(srWebsites.getWebLocation());
				newPPpUserCommonPad.setMemo(srWebsites.getMemo());

				resultPPUserCommonPadList.add(newPPpUserCommonPad);
			}
			request.setAttribute("indexWebsitesList", resultPPUserCommonPadList);
		}
		return new ModelAndView("index");

	}

	public WebappDao getWebappDao() {
		return webappDao;
	}

	public void setWebappDao(WebappDao webappDao) {
		this.webappDao = webappDao;
	}

}
